/**
 * 会员级别分类
 * */


var pages = {
    start: 1, limit: pageSize, total: 0,
    url: '/admin/ulevel.html'
};
pages.start = getParamByUrl('start');
pages.start = pages.start ? pages.start : 1;


(function () {
})();


//分类列表
function getList(){
    $.ajax({
        type: apis.levelIndex.method,
        url: apis.levelIndex.url,
        data: { start: pages.start, limit: pages.limit },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#pageTotal').html(d.length);
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].ver + '</td>';
                h += '<td>' + d[i].price + '</td>';
                h += '<td>';
                if (d[i].icon == '') {
                    h += '-';
                } else {
                    h += '<img src="' + d[i].icon + '" height="100"/>';
                }
                h += '</td>';
                h += '<td>' + d[i].tname + '</td>';
                h += '<td>' + d[i].real_cost + '</td>';
                h += '<td>' + d[i].discount + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="ulevel_edit(\'编辑\',\'ulevel-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
                h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="ulevel_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
                h += '删除 <i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(v){
    $.ajax({
        type: apis.levelOne.method,
        url: apis.levelOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#title')[0].value = d.title;
            $('#ver')[0].value = d.ver;
            $('#bigShow').html('<img src="/' + d.icon + '" height="200"/><input type="hidden" id="img" value="' + d.icon + '"/>');
            $('#describe')[0].value = d.describe;
            $('#price')[0].value = d.price;
            var h = '';
            if (d.type == 1) {
                h += '<option value="1" selected>黄金|铂金|钻石【自动】</option>';
                h += '<option value="2">SVIP【手动】</option>';
            } else {
                h += '<option value="1">黄金|铂金|钻石【自动】</option>';
                h += '<option value="2" selected>SVIP【手动】</option>';
            }
            $('#type').html(h);
            $('#cost')[0].value = d.real_cost;
            $('#discount')[0].value = d.discount;
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function ulevel_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//删除
function ulevel_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.levelDel.method,
            url: apis.levelDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                //layer.msg('已删除!',{icon: 1,time: 1000});
                msgPop({msg: '已删除'});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).width(e.w).height(e.h);
}


//保存更新
function save(){
    var formData = {
        id: id,
        title: $('#title').val(),
        ver: $('#ver').val(),
        icon: $('#img').val(),
        describe: $('#describe').val(),
        price: $('#price').val(),
        type: $('#type').val(),
        real_cost: $('#cost').val(),
        discount: $('#discount').val(),
    };
    console.log(formData);
    if (!formData.title) {
        msgPop({msg: '等级名称必须的'}); return
    }
    //if (!formData.icon) {
    //    msgPop({msg: '等级图标必须的'}); return
    //}
    //if (!formData.price) {
    //    msgPop({msg: '等级价格必须的'}); return
    //}
    //console.log(formData); return
    $.ajax({
        type: apis.levelModify.method,
        url: apis.levelModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var operMsg = id ? '修改' : '添加';
            msgPop({msg: operMsg + '成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}